package com.bupt.controller;

import com.bupt.bean.Device;
import com.bupt.config.QueryPageRequest;
import com.bupt.config.QueryResponseResult;
import com.bupt.service.DeviceService;
import com.bupt.service.PageService;
import com.bupt.utils.ExcelUtils;
import com.bupt.vo.DeviceLogExportVo;
import com.bupt.vo.SysConst;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(value="设备日志",description="设备日志查询、导出、日志类型、通过设备类型查询设备")
@RestController
public class DeviceLogController {

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private PageService pageService;

    @ApiOperation("分页查询设备日志列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页码", required = true, paramType = "path", dataType = "int"), @ApiImplicitParam(name = "size", value = "每页记录数", required = true, paramType = "path", dataType = "int")})
    @GetMapping("/deviceLogPage/{page}/{size}")
    public QueryResponseResult eventPage(@PathVariable("page") int page, @PathVariable("size") int size, QueryPageRequest queryPageRequest) {
        return pageService.findDeviceLogList(page, size, queryPageRequest);
    }

    @ApiOperation(value = "设备日志导出",produces = "application/octet-stream")
    @RequestMapping(value="/deviceLogPage/exportExcelDeviceLog", method = RequestMethod.GET,headers = "Accept=application/octet-stream")
    public void exportExcelUser( QueryPageRequest queryPageRequest, HttpServletResponse res) throws IOException {
        List deviceLogList = pageService.findDeviceLogList(0, 9999, queryPageRequest).getQueryResult().getList();
        List<DeviceLogExportVo> deviceLogExportVoList = new ArrayList<DeviceLogExportVo>();
        for (int i = 0; i < deviceLogList.size(); i++) {
            Map<String, Object> deviceLogVo = (Map<String, Object>) deviceLogList.get(i);
            DeviceLogExportVo deviceLogExportVo = new DeviceLogExportVo();
            deviceLogExportVo.setDeviceTypeName(deviceLogVo.get("device_type_name").toString());
            deviceLogExportVo.setDeviceName(deviceLogVo.get("device_name").toString());
            deviceLogExportVo.setConstDesc(deviceLogVo.get("const_desc").toString());
            deviceLogExportVo.setChannelIndex(deviceLogVo.get("channel_index").toString());
            deviceLogExportVo.setContent(deviceLogVo.get("content").toString());
            deviceLogExportVo.setCreatTime(deviceLogVo.get("creat_time").toString());
            deviceLogExportVoList.add(deviceLogExportVo);
        }

        ExcelUtils.exportExcel(deviceLogExportVoList, "设备日志表", "设备日志", DeviceLogExportVo.class, "deviceLog", res);
    }





    @ApiOperation("日志类型")
    @GetMapping("/deviceLog/LogType")
    public List<SysConst> LogType() {
        return deviceService.logType();
    }

    @ApiOperation("通过设备类型查询设备")
    @GetMapping("/deviceLog/DeviceByTypeId/{deviceTypeId}")
    public List<Device> DeviceByTypeId(@PathVariable("deviceTypeId") Integer deviceTypeId) {
        return deviceService.DeviceByTypeId(deviceTypeId);
    }

}
